<template>
    <div>

      <div style="padding: 8vh 5vw;">
          
          
          <div style="font-size: 5vw;text-align: start;font-weight: bold;margin-bottom:20px;">设备绑定</div>
          <van-field v-model="username" type="tel" placeholder="请输入预留的手机号" style="background-color:#f3f4f8;margin: 8px 0;border-radius: 4px;" />
          <van-field v-model="password" type="password"  placeholder="请设置新的密码" style="background-color:#f3f4f8;border-radius: 4px;"/>
          <van-button type="info" style="margin-top:20px;width: 100%;" @click="binding">绑定</van-button>

      </div>


    </div>
    
</template>

<script>
import { login,checkLoginStatus } from "@/api/system/login";


export default {
  data() {
    return {
      show: false,
      username:'',
      password:'',
      code: 'aaa123',
      state:'',
      url:null
    };
  },

  created(){
    this.url = this.$route.query.url;
    checkLoginStatus().then(res=>{
      console.log("已经登录");
      this.$router.push('loginsuccess')
    }).catch(error=>{
      console.log("未登录");
    })

    const searchParams = new URLSearchParams(window.location.search);
    this.code = searchParams.get('code')
    this.state = searchParams.get('state')
    console.log('this.state',this.state);
    // const url1 = location.href;
    // var reg1 = new RegExp("login"); // 加'g'，删除字符串里所有的"a"
    // const url2 = url1.replace(reg1,"");
    //this.$toast(location.href);
    window.localStorage.setItem("loginUrl",location.href)


    
  },

  methods: {

    checkLoginStatus(){
      checkLoginStatus().then(res=>{
        this.$router.push('loginsuccess');
      }).catch(error=>{
        this.toLogin();
      })
    },

    toLogin(){

      
      const username = this.username.trim()
      const password = this.password

      
      binding(username, password,this.code).then(res => {
        console.log('token',res.loginResultMap);
        const token = res.loginResultMap.token
        const userId = res.loginResultMap.userId
        window.localStorage.setItem("Token",token)
        window.localStorage.setItem("userId",userId)
        if(this.state==null){
          this.$router.push('loginsuccess')
        }else{
          this.$router.push(this.state)
        }
        
      }).catch(error => {
        
      })
    },

    showPopup() {
      this.show = true;
    },

    onClickLeft(){

    },


    
  },
};
</script>